﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Hxsoft
{
	public static partial class StringExtensions
	{
		/// <summary>
		/// 将字符串拆分成数组。
		/// </summary>
		/// <param name="o">进行拆分的字符串。</param>
		/// <param name="separator">拆分的分隔符。</param>
		/// <returns>拆分后的字符串数组。</returns>
		public static string[] Split(this string o, string separator)
		{
			return o.Split(separator, StringSplitOptions.None);
		}

		/// <summary>
		/// 将字符串拆分成数组。
		/// </summary>
		/// <param name="o">进行拆分的字符串。</param>
		/// <param name="separator">拆分的分隔符。</param>
		/// <param name="options"></param>
		/// <returns>拆分后的字符串数组。</returns>
		public static string[] Split(this string o, string separator, StringSplitOptions options)
		{
			return o.Split(new string[] { separator }, options);
		}

		/// <summary>
		/// 将字符串进行 Html 编码。
		/// </summary>
		/// <param name="str">进行编码的字符串。</param>
		/// <returns>编码后的字符串。</returns>
		public static string HtmlEncode(this string str)
		{
			if (string.IsNullOrEmpty(str)) return str;
			return HttpUtility.HtmlEncode(str).Replace(" ", "&nbsp;").Replace("\n", "<br />");
		}

		/// <summary>
		/// 将字符串进行 Html 反编码。
		/// </summary>
		/// <param name="str">进行反编码的字符串。</param>
		/// <returns>反编码后的字符串。</returns>
		public static string HtmlDecode(this string str)
		{
			if (string.IsNullOrEmpty(str)) return str;
			return HttpUtility.HtmlDecode(str.Replace("<br />", "\n").Replace("&nbsp;", " "));
		}

		/// <summary>
		/// 用分隔符将集合中的元素拼接成字符串形式，并在两端添加分隔符包装。
		/// </summary>
		/// <typeparam name="T">集合元素的类型。</typeparam>
		/// <param name="c">进行操作的集合。</param>
		/// <param name="separator">拼接的分隔符。</param>
		/// <param name="options">拼接的选项。</param>
		/// <returns>拼接后的字符串。</returns>
		public static string CasingCompound<T>(this ICollection<T> c, string separator, StringSplitOptions options)
		{
			return Pack(Compound<T>(c, separator, options), separator);
		}

		/// <summary>
		/// 用分隔符将集合中的元素拼接成字符串形式，并在两端添加分隔符包装。
		/// </summary>
		/// <typeparam name="T">集合元素的类型。</typeparam>
		/// <param name="c">进行操作的集合。</param>
		/// <param name="separator">拼接的分隔符。</param>
		/// <returns>拼接后的字符串。</returns>
		public static string CasingCompound<T>(this ICollection<T> c, string separator)
		{
			return CasingCompound<T>(c, separator, StringSplitOptions.None);
		}

		/// <summary>
		/// 用分隔符将字符串分隔为一个集合，并去除两端的包装。
		/// </summary>
		/// <typeparam name="T">集合元素的类型。</typeparam>
		/// <param name="source">进行分隔的字符串。</param>
		/// <param name="separator">分隔符。</param>
		/// <param name="options">分隔的选项。</param>
		/// <returns>分隔后的集合。</returns>
		public static T[] CasingDecompound<T>(this string source, string separator, StringSplitOptions options)
		{
			return Decompound<T>(Unpack(source, separator), separator, options);
		}

		/// <summary>
		/// 用分隔符将字符串分隔为一个集合，并去除两端的包装。
		/// </summary>
		/// <typeparam name="T">集合元素的类型。</typeparam>
		/// <param name="source">进行分隔的字符串。</param>
		/// <param name="separator">分隔符。</param>
		/// <returns>分隔后的集合。</returns>
		public static T[] CasingDecompound<T>(this string source, string separator)
		{
			return CasingDecompound<T>(source, separator, StringSplitOptions.None);
		}

		/// <summary>
		/// 用分隔符将集合中的元素拼接成字符串形式。
		/// </summary>
		/// <typeparam name="T">集合元素的类型。</typeparam>
		/// <param name="c">进行操作的集合。</param>
		/// <param name="separator">拼接的分隔符。</param>
		/// <param name="options">拼接的选项。</param>
		/// <returns>拼接后的字符串。</returns>
		public static string Compound<T>(this ICollection<T> c, string separator, StringSplitOptions options)
		{
			if (c == null) return null;
			string result = null;
			foreach (T item in c)
			{
				string strItem = item.ToString();
				if (options == StringSplitOptions.RemoveEmptyEntries && string.IsNullOrEmpty(strItem)) continue;

				if (result == null)
					result = strItem;
				else
					result += separator + strItem;
			}
			return result;
		}

		/// <summary>
		/// 用分隔符将集合中的元素拼接成字符串形式。
		/// </summary>
		/// <typeparam name="T">集合元素的类型。</typeparam>
		/// <param name="c">进行操作的集合。</param>
		/// <param name="separator">拼接的分隔符。</param>
		/// <returns>拼接后的字符串。</returns>
		public static string Compound<T>(this ICollection<T> c, string separator)
		{
			return Compound<T>(c, separator, StringSplitOptions.None);
		}

		/// <summary>
		/// 用分隔符将字符串分隔为一个集合。
		/// </summary>
		/// <typeparam name="T">集合的元素。</typeparam>
		/// <param name="source">进行分隔的字符串。</param>
		/// <param name="separator">分隔符。</param>
		/// <param name="options">分隔的选项。</param>
		/// <returns>分隔后的集合。</returns>
		public static T[] Decompound<T>(this string source, string separator, StringSplitOptions options)
		{
			List<T> c = new List<T>();

			if (!string.IsNullOrEmpty(source))
			{
				string[] elements = source.Split(new string[] { separator }, options);

				foreach (string element in elements)
				{
					T item = (T)Convert.ChangeType(element, typeof(T));
					c.Add(item);
				}
			}

			return c.ToArray();
		}

		/// <summary>
		/// 用分隔符将字符串分隔为一个集合。
		/// </summary>
		/// <typeparam name="T">集合的元素。</typeparam>
		/// <param name="source">进行分隔的字符串。</param>
		/// <param name="separator">分隔符。</param>
		/// <returns>分隔后的集合。</returns>
		public static T[] Decompound<T>(this string source, string separator)
		{
			return Decompound<T>(source, separator, StringSplitOptions.None);
		}
	}
}
